<template>
  <div class="detail">
    <el-card class="detailcard">
      <div class="btnbox">
        <el-button @click="toprint" class="btn" size="mini" type="primary">
          打印
        </el-button>
      </div>
      <el-tabs v-model="activeName">
        <el-tab-pane label="登录用户设置" name="first">
          <el-form label-width="80px" class="myfrom" :model="userInfo">
            <el-form-item label="姓名:">
              <el-input v-model="userInfo.username"></el-input>
            </el-form-item>
            <el-form-item label="密码:">
              <el-input v-model="userInfo.password"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="update">更新</el-button>
              <el-button>取消</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="个人详情" name="second"
          ><userinfo></userinfo
        ></el-tab-pane>
        <el-tab-pane label="岗位信息" name="third"
          ><jobinfo></jobinfo
        ></el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script>
import { getuserinfoApi, setUserInfoApi } from '../../../api/employees'
import userinfo from '../components/user-info.vue'
import jobinfo from '../components/job-info.vue'
export default {
  data () {
    return {
      // 默认选中
      activeName: 'first',
      // id
      id: this.$route.query.id,
      // 表单数据
      userInfo: {
        username: '',
        password: ''
      },
      info: {}
    }
  },
  methods: {
    // 获取基本信息
    async getuserinfo () {
      const res = await getuserinfoApi(this.id)
      this.userInfo.username = res.username
      this.info = res
      console.log(res)
    },
    // 点击修改用户信息，并跳转回员工页面
    async update () {
      try {
        setUserInfoApi({
          ...this.info,
          ...this.userInfo
        })
        this.$message.success('更新成功')
        this.$router.push('/employees')
      } catch (error) {
        this.$message.error(error.message)
      }
    },
    //跳转到详情页面
    toprint () {
      this.$router.push(`/employees/print/${this.id}?type='${this.activeName}`)
    }
  },
  created () {
    this.getuserinfo()
  },
  components: {
    userinfo,
    jobinfo
  }
}
</script>

<style lang="scss">
.detail {
  .detailcard {
    margin-top: 20px;
    .myfrom {
      width: 400px;
      margin: 0 auto;
    }
  }
  .btnbox {
    position: relative;
    .btn {
      position: absolute;
      right: 0px;
      z-index: 20;
    }
  }
}
</style>
